clear all
cd "~/Dropbox/Historical Displacement and Ethnicity - Afghanistan/REPOSITORY/"
use "replication/data/input/FOGHORN.dta", clear

* Global variables
global m1m2 `" "Model I" "Model II" "'
global controls urban age yearsedu college unemployed elder headhousehold married econ_status

*------------------------------------------------------------
* Section 1: Export to data/intermediate for plotting in R
*------------------------------------------------------------

* Fit different regressions where the dependent variables are
* the effectiveness of government and institutions, and export
* results for plotting

*------------------------------------------------------------
* Provincial Council effectiveness
*------------------------------------------------------------

* 1. Main results (exclude insurgent attacks)
reghdfe prov_council_effective displaced_pashtun nonpashtun_displaced nonpashtun_nondisplaced ///
    $controls [pw=surv_weight], abs(PROVID m3) vce(cluster DISTID#m3)
estimates store m1_1
mean prov_council_effective [pw=surv_weight] if nondisplaced_pashtun == 1
global mean1_1 = r(table)[1,1]

* 2. Main results (include insurgent attacks)
reghdfe prov_council_effective displaced_pashtun nonpashtun_displaced nonpashtun_nondisplaced ///
    $controls ln_insurgent_attacks_all [pw=surv_weight], abs(PROVID m3) vce(cluster DISTID#m3)
estimates store m1_2
mean prov_council_effective [pw=surv_weight] if nondisplaced_pashtun == 1
global mean1_2 = r(table)[1,1]

* 3. Excluding nondisplaced Pashtuns in the North (compare displaced Pashtuns only to those in the South and West)
reghdfe prov_council_effective displaced_pashtun nonpashtun_displaced nonpashtun_nondisplaced ///
    $controls ln_insurgent_attacks_all [pw=surv_weight] if nondisplaced_pashtun_north == 0, ///
    abs(PROVID m3) vce(cluster DISTID#m3)
estimates store m1_3
mean prov_council_effective [pw=surv_weight] if nondisplaced_pashtun_south == 1
global mean1_3 = r(table)[1,1]

* 4. Comparing Pashtuns in the North to all others
reghdfe prov_council_effective all_pashtun_north ///
    $controls ln_insurgent_attacks_all [pw=surv_weight], abs(PROVID m3) vce(cluster DISTID#m3)
estimates store m1_4
mean prov_council_effective [pw=surv_weight] if all_pashtun_north == 0
global mean1_4 = r(table)[1,1]

* 5. Only including Afghanis in the North
reghdfe prov_council_effective displaced_pashtun nonpashtun_displaced ///
    $controls ln_insurgent_attacks_all [pw=surv_weight] if north_prov == 1, ///
    abs(PROVID m3) vce(cluster DISTID#m3)
estimates store m1_5
mean prov_council_effective [pw=surv_weight] if north_prov == 1 & displaced_pashtun == 0 & nonpashtun_displaced == 0
global mean1_5 = r(table)[1,1]

* 6. Alternative measure
reghdfe prov_council_effective displaced_pashtun2 nonpashtun_displaced nonpashtun_nondisplaced ///
    $controls ln_insurgent_attacks_all [pw=surv_weight], abs(PROVID m3) vce(cluster DISTID#m3)
estimates store m1_6
mean prov_council_effective [pw=surv_weight] if displaced_pashtun2 == 0 & pashtun == 1
global mean1_6 = r(table)[1,1]

*------------------------------------------------------------
* Central government effectiveness
*------------------------------------------------------------

* 1. Main results (exclude insurgent attacks)
reghdfe cent_govt_effective displaced_pashtun nonpashtun_displaced nonpashtun_nondisplaced ///
    $controls [pw=surv_weight], abs(PROVID m3) vce(cluster DISTID#m3)
estimates store m2_1
mean cent_govt_effective [pw=surv_weight] if nondisplaced_pashtun == 1
global mean2_1 = r(table)[1,1]

* 2. Main results (include insurgent attacks)
reghdfe cent_govt_effective displaced_pashtun nonpashtun_displaced nonpashtun_nondisplaced ///
    $controls ln_insurgent_attacks_all [pw=surv_weight], abs(PROVID m3) vce(cluster DISTID#m3)
estimates store m2_2
mean cent_govt_effective [pw=surv_weight] if nondisplaced_pashtun == 1
global mean2_2 = r(table)[1,1]

* 3. Excluding nondisplaced Pashtuns in the North
reghdfe cent_govt_effective displaced_pashtun nonpashtun_displaced nonpashtun_nondisplaced ///
    $controls ln_insurgent_attacks_all [pw=surv_weight] if nondisplaced_pashtun_north == 0, ///
    abs(PROVID m3) vce(cluster DISTID#m3)
estimates store m2_3
mean cent_govt_effective [pw=surv_weight] if nondisplaced_pashtun_south == 1
global mean2_3 = r(table)[1,1]

* 4. Comparing Pashtuns in the North to all others
reghdfe cent_govt_effective all_pashtun_north ///
    $controls ln_insurgent_attacks_all [pw=surv_weight], abs(PROVID m3) vce(cluster DISTID#m3)
estimates store m2_4
mean cent_govt_effective [pw=surv_weight] if all_pashtun_north == 0
global mean2_4 = r(table)[1,1]

* 5. Only including Afghanis in the North
reghdfe cent_govt_effective displaced_pashtun nonpashtun_displaced ///
    $controls ln_insurgent_attacks_all [pw=surv_weight] if north_prov == 1, ///
    abs(PROVID m3) vce(cluster DISTID#m3)
estimates store m2_5
mean cent_govt_effective [pw=surv_weight] if north_prov == 1 & displaced_pashtun == 0 & nonpashtun_displaced == 0
global mean2_5 = r(table)[1,1]

* 6. Alternative measure
reghdfe cent_govt_effective displaced_pashtun2 nonpashtun_displaced nonpashtun_nondisplaced ///
    $controls ln_insurgent_attacks_all [pw=surv_weight], abs(PROVID m3) vce(cluster DISTID#m3)
estimates store m2_6
mean cent_govt_effective [pw=surv_weight] if displaced_pashtun2 == 0 & pashtun == 1
global mean2_6 = r(table)[1,1]

*------------------------------------------------------------
* Ulama effectiveness
*------------------------------------------------------------

* 1. Main results (exclude insurgent attacks)
reghdfe ulama_effective displaced_pashtun nonpashtun_displaced nonpashtun_nondisplaced ///
    $controls [pw=surv_weight], abs(PROVID m3) vce(cluster DISTID#m3)
estimates store m3_1
mean ulama_effective [pw=surv_weight] if nondisplaced_pashtun == 1
global mean3_1 = r(table)[1,1]

* 2. Main results (include insurgent attacks)
reghdfe ulama_effective displaced_pashtun nonpashtun_displaced nonpashtun_nondisplaced ///
    $controls ln_insurgent_attacks_all [pw=surv_weight], abs(PROVID m3) vce(cluster DISTID#m3)
estimates store m3_2
mean ulama_effective [pw=surv_weight] if nondisplaced_pashtun == 1
global mean3_2 = r(table)[1,1]

* 3. Excluding nondisplaced Pashtuns in the North
reghdfe ulama_effective displaced_pashtun nonpashtun_displaced nonpashtun_nondisplaced ///
    $controls ln_insurgent_attacks_all [pw=surv_weight] if nondisplaced_pashtun_north == 0, ///
    abs(PROVID m3) vce(cluster DISTID#m3)
estimates store m3_3
mean ulama_effective [pw=surv_weight] if nondisplaced_pashtun_south == 1
global mean3_3 = r(table)[1,1]

* 4. Comparing Pashtuns in the North to all others
reghdfe ulama_effective all_pashtun_north ///
    $controls ln_insurgent_attacks_all [pw=surv_weight], abs(PROVID m3) vce(cluster DISTID#m3)
estimates store m3_4
mean ulama_effective [pw=surv_weight] if all_pashtun_north == 0
global mean3_4 = r(table)[1,1]

* 5. Only including Afghanis in the North
reghdfe ulama_effective displaced_pashtun nonpashtun_displaced ///
    $controls ln_insurgent_attacks_all [pw=surv_weight] if north_prov == 1, ///
    abs(PROVID m3) vce(cluster DISTID#m3)
estimates store m3_5
mean ulama_effective [pw=surv_weight] if north_prov == 1 & displaced_pashtun == 0 & nonpashtun_displaced == 0
global mean3_5 = r(table)[1,1]

* 6. Alternative measure
reghdfe ulama_effective displaced_pashtun2 nonpashtun_displaced nonpashtun_nondisplaced ///
    $controls ln_insurgent_attacks_all [pw=surv_weight], abs(PROVID m3) vce(cluster DISTID#m3)
estimates store m3_6
mean ulama_effective [pw=surv_weight] if displaced_pashtun2 == 0 & pashtun == 1
global mean3_6 = r(table)[1,1]

*------------------------------------------------------------
* Local government community relations
*------------------------------------------------------------

* 1. Main results (exclude insurgent attacks)
reghdfe locgovt_communityrelation displaced_pashtun nonpashtun_displaced nonpashtun_nondisplaced ///
    $controls [pw=surv_weight], abs(PROVID m3) vce(cluster DISTID#m3)
estimates store m4_1
mean locgovt_communityrelation [pw=surv_weight] if nondisplaced_pashtun == 1
global mean4_1 = r(table)[1,1]

* 2. Main results (include insurgent attacks)
reghdfe locgovt_communityrelation displaced_pashtun nonpashtun_displaced nonpashtun_nondisplaced ///
    $controls ln_insurgent_attacks_all [pw=surv_weight], abs(PROVID m3) vce(cluster DISTID#m3)
estimates store m4_2
mean locgovt_communityrelation [pw=surv_weight] if nondisplaced_pashtun == 1
global mean4_2 = r(table)[1,1]

* 3. Excluding nondisplaced Pashtuns in the North
reghdfe locgovt_communityrelation displaced_pashtun nonpashtun_displaced nonpashtun_nondisplaced ///
    $controls ln_insurgent_attacks_all [pw=surv_weight] if nondisplaced_pashtun_north == 0, ///
    abs(PROVID m3) vce(cluster DISTID#m3)
estimates store m4_3
mean locgovt_communityrelation [pw=surv_weight] if nondisplaced_pashtun_south == 1
global mean4_3 = r(table)[1,1]

* 4. Comparing Pashtuns in the North to all others
reghdfe locgovt_communityrelation all_pashtun_north ///
    $controls ln_insurgent_attacks_all [pw=surv_weight], abs(PROVID m3) vce(cluster DISTID#m3)
estimates store m4_4
mean locgovt_communityrelation [pw=surv_weight] if all_pashtun_north == 0
global mean4_4 = r(table)[1,1]

* 5. Only including Afghanis in the North
reghdfe locgovt_communityrelation displaced_pashtun nonpashtun_displaced ///
    $controls ln_insurgent_attacks_all [pw=surv_weight] if north_prov == 1, ///
    abs(PROVID m3) vce(cluster DISTID#m3)
estimates store m4_5
mean locgovt_communityrelation [pw=surv_weight] if north_prov == 1 & displaced_pashtun == 0 & nonpashtun_displaced == 0
global mean4_5 = r(table)[1,1]

* 6. Alternative measure
reghdfe locgovt_communityrelation displaced_pashtun2 nonpashtun_displaced nonpashtun_nondisplaced ///
    $controls ln_insurgent_attacks_all [pw=surv_weight], abs(PROVID m3) vce(cluster DISTID#m3)
estimates store m4_6
mean locgovt_communityrelation [pw=surv_weight] if displaced_pashtun2 == 0 & pashtun == 1
global mean4_6 = r(table)[1,1]

*------------------------------------------------------------
* Export results for plotting
*------------------------------------------------------------
esttab m1_1 m1_2 m1_3 m1_4 m1_5 m1_6 ///
       m2_1 m2_2 m2_3 m2_4 m2_5 m2_6 ///
       m3_1 m3_2 m3_3 m3_4 m3_5 m3_6 ///
       m4_1 m4_2 m4_3 m4_4 m4_5 m4_6 ///
    using "replication/data/intermediate/FOGHORNresults_effective.csv", ///
    cells(b(fmt(3)) se(fmt(3))) ///
    label mlabels("prov_council1" "prov_council2" "prov_council3" ///
                  "prov_council4" "prov_council5" "prov_council6" ///
                  "cent_govt1" "cent_govt2" "cent_govt3" ///
                  "cent_govt4" "cent_govt5" "cent_govt6" ///
                  "ulama1" "ulama2" "ulama3" ///
                  "ulama4" "ulama5" "ulama6" ///
                  "locgovt_communityrelation1" "locgovt_communityrelation2" ///
                  "locgovt_communityrelation3" "locgovt_communityrelation4" ///
                  "locgovt_communityrelation5" "locgovt_communityrelation6") csv

* Create a matrix with mean values
matrix means = ($mean1_1, $mean1_2, $mean1_3, $mean1_4, $mean1_5, $mean1_6, ///
                 $mean2_1, $mean2_2, $mean2_3, $mean2_4, $mean2_5, $mean2_6, ///
                 $mean3_1, $mean3_2, $mean3_3, $mean3_4, $mean3_5, $mean3_6, ///
                 $mean4_1, $mean4_2, $mean4_3, $mean4_4, $mean4_5, $mean4_6)

* Add column names for clarity
matrix colnames means = prov_council1 prov_council2 prov_council3 ///
                       prov_council4 prov_council5 prov_council6 ///
                       cent_govt1 cent_govt2 cent_govt3 ///
                       cent_govt4 cent_govt5 cent_govt6 ///
                       ulama1 ulama2 ulama3 ///
                       ulama4 ulama5 ulama6 ///
                       locgovt_communityrelation1 locgovt_communityrelation2 ///
                       locgovt_communityrelation3 locgovt_communityrelation4 ///
                       locgovt_communityrelation5 locgovt_communityrelation6

* Export the matrix of means
esttab matrix(means) using "replication/data/intermediate/FOGHORNresults_effective_means.csv", replace

* Drop temporary macros
macro drop mean1_1
macro drop mean1_2
macro drop mean1_3
macro drop mean1_4
macro drop mean1_5
macro drop mean1_6
macro drop mean2_1
macro drop mean2_2
macro drop mean2_3
macro drop mean2_4
macro drop mean2_5
macro drop mean2_6
macro drop mean3_1
macro drop mean3_2
macro drop mean3_3
macro drop mean3_4
macro drop mean3_5
macro drop mean3_6
macro drop mean4_1
macro drop mean4_2
macro drop mean4_3
macro drop mean4_4
macro drop mean4_5
macro drop mean4_6


* Clear previous estimates
estimates clear

*------------------------------------------------------------
* Provincial Government Legitimacy
*------------------------------------------------------------

* 1. Main result
estimates clear
reghdfe provgovt_legit displaced_pashtun nonpashtun_displaced nonpashtun_nondisplaced ///
    $controls [pw=surv_weight], abs(PROVID m3) vce(cluster DISTID#m3)
estimates store m1_1
mean provgovt_legit [pw=surv_weight] if nondisplaced_pashtun == 1
global mean1_1 = r(table)[1,1]

* 2. Include insurgent attacks
reghdfe provgovt_legit displaced_pashtun nonpashtun_displaced nonpashtun_nondisplaced ///
    $controls ln_insurgent_attacks_all [pw=surv_weight], abs(PROVID m3) vce(cluster DISTID#m3)
estimates store m1_2
mean provgovt_legit [pw=surv_weight] if nondisplaced_pashtun == 1
global mean1_2 = r(table)[1,1]

* 3. Excluding nondisplaced Pashtuns in the North
reghdfe provgovt_legit displaced_pashtun nonpashtun_displaced nonpashtun_nondisplaced ///
    $controls ln_insurgent_attacks_all [pw=surv_weight] if nondisplaced_pashtun_north == 0, ///
    abs(PROVID m3) vce(cluster DISTID#m3)
estimates store m1_3
mean provgovt_legit [pw=surv_weight] if nondisplaced_pashtun_south == 1
global mean1_3 = r(table)[1,1]

* 4. Comparing Pashtuns in the North to all others
reghdfe provgovt_legit all_pashtun_north $controls ln_insurgent_attacks_all [pw=surv_weight], ///
    abs(PROVID m3) vce(cluster DISTID#m3)
estimates store m1_4
mean provgovt_legit [pw=surv_weight] if all_pashtun_north == 0
global mean1_4 = r(table)[1,1]

* 5. Only including Afghanis in the North
reghdfe provgovt_legit displaced_pashtun nonpashtun_displaced $controls ///
    ln_insurgent_attacks_all [pw=surv_weight] if north_prov == 1, ///
    abs(PROVID m3) vce(cluster DISTID#m3)
estimates store m1_5
mean provgovt_legit [pw=surv_weight] if north_prov == 1 & displaced_pashtun == 0 & nonpashtun_displaced == 0
global mean1_5 = r(table)[1,1]

* 6. Alternative measure
reghdfe provgovt_legit displaced_pashtun2 nonpashtun_displaced nonpashtun_nondisplaced ///
    $controls ln_insurgent_attacks_all [pw=surv_weight], abs(PROVID m3) vce(cluster DISTID#m3)
estimates store m1_6
mean provgovt_legit [pw=surv_weight] if displaced_pashtun2 == 0 & pashtun == 1
global mean1_6 = r(table)[1,1]


*------------------------------------------------------------
* Wolesi Jirga legitimacy
*------------------------------------------------------------

reghdfe jirga_legit displaced_pashtun nonpashtun_displaced nonpashtun_nondisplaced ///
    $controls [pw=surv_weight], abs(PROVID m3) vce(cluster DISTID#m3)
estimates store m2_1
mean jirga_legit [pw=surv_weight] if nondisplaced_pashtun == 1
global mean2_1 = r(table)[1,1]

reghdfe jirga_legit displaced_pashtun nonpashtun_displaced nonpashtun_nondisplaced ///
    $controls ln_insurgent_attacks_all [pw=surv_weight], abs(PROVID m3) vce(cluster DISTID#m3)
estimates store m2_2
mean jirga_legit [pw=surv_weight] if nondisplaced_pashtun == 1
global mean2_2 = r(table)[1,1]

* 3. Excluding nondisplaced Pashtuns in the North
reghdfe jirga_legit displaced_pashtun nonpashtun_displaced nonpashtun_nondisplaced ///
    $controls ln_insurgent_attacks_all [pw=surv_weight] if nondisplaced_pashtun_north == 0, ///
    abs(PROVID m3) vce(cluster DISTID#m3)
estimates store m2_3
mean jirga_legit [pw=surv_weight] if nondisplaced_pashtun_south == 1
global mean2_3 = r(table)[1,1]

* 4. Comparing Pashtuns in the North to all others
reghdfe jirga_legit all_pashtun_north $controls ln_insurgent_attacks_all [pw=surv_weight], ///
    abs(PROVID m3) vce(cluster DISTID#m3)
estimates store m2_4
mean jirga_legit [pw=surv_weight] if all_pashtun_north == 0
global mean2_4 = r(table)[1,1]

* 5. Only including Afghanis in the North
reghdfe jirga_legit displaced_pashtun nonpashtun_displaced $controls ///
    ln_insurgent_attacks_all [pw=surv_weight] if north_prov == 1, ///
    abs(PROVID m3) vce(cluster DISTID#m3)
estimates store m2_5
mean jirga_legit [pw=surv_weight] if north_prov == 1 & displaced_pashtun == 0 & nonpashtun_displaced == 0
global mean2_5 = r(table)[1,1]

* 6. Alternative measure
reghdfe jirga_legit displaced_pashtun2 nonpashtun_displaced nonpashtun_nondisplaced ///
    $controls ln_insurgent_attacks_all [pw=surv_weight], abs(PROVID m3) vce(cluster DISTID#m3)
estimates store m2_6
mean jirga_legit [pw=surv_weight] if displaced_pashtun2 == 0 & pashtun == 1
global mean2_6 = r(table)[1,1]


*------------------------------------------------------------
* Central government legitimacy
*------------------------------------------------------------

reghdfe centgovt_legit displaced_pashtun nonpashtun_displaced nonpashtun_nondisplaced ///
    $controls [pw=surv_weight], abs(PROVID m3) vce(cluster DISTID#m3)
estimates store m3_1
mean centgovt_legit [pw=surv_weight] if nondisplaced_pashtun == 1
global mean3_1 = r(table)[1,1]

reghdfe centgovt_legit displaced_pashtun nonpashtun_displaced nonpashtun_nondisplaced ///
    $controls ln_insurgent_attacks_all [pw=surv_weight], abs(PROVID m3) vce(cluster DISTID#m3)
estimates store m3_2
mean centgovt_legit [pw=surv_weight] if nondisplaced_pashtun == 1
global mean3_2 = r(table)[1,1]

* 3. Excluding nondisplaced Pashtuns in the North
reghdfe centgovt_legit displaced_pashtun nonpashtun_displaced nonpashtun_nondisplaced ///
    $controls ln_insurgent_attacks_all [pw=surv_weight] if nondisplaced_pashtun_north == 0, ///
    abs(PROVID m3) vce(cluster DISTID#m3)
estimates store m3_3
mean centgovt_legit [pw=surv_weight] if nondisplaced_pashtun_south == 1
global mean3_3 = r(table)[1,1]

* 4. Comparing Pashtuns in the North to all others
reghdfe centgovt_legit all_pashtun_north $controls ln_insurgent_attacks_all [pw=surv_weight], ///
    abs(PROVID m3) vce(cluster DISTID#m3)
estimates store m3_4
mean centgovt_legit [pw=surv_weight] if all_pashtun_north == 0
global mean3_4 = r(table)[1,1]

* 5. Only including Afghanis in the North
reghdfe centgovt_legit displaced_pashtun nonpashtun_displaced $controls ///
    ln_insurgent_attacks_all [pw=surv_weight] if north_prov == 1, ///
    abs(PROVID m3) vce(cluster DISTID#m3)
estimates store m3_5
mean centgovt_legit [pw=surv_weight] if north_prov == 1 & displaced_pashtun == 0 & nonpashtun_displaced == 0
global mean3_5 = r(table)[1,1]

* 6. Alternative measure
reghdfe centgovt_legit displaced_pashtun2 nonpashtun_displaced nonpashtun_nondisplaced ///
    $controls ln_insurgent_attacks_all [pw=surv_weight], abs(PROVID m3) vce(cluster DISTID#m3)
estimates store m3_6
mean centgovt_legit [pw=surv_weight] if displaced_pashtun2 == 0 & pashtun == 1
global mean3_6 = r(table)[1,1]


*------------------------------------------------------------
* Judicial fairness
*------------------------------------------------------------

reghdfe govtcourts_fair displaced_pashtun nonpashtun_displaced nonpashtun_nondisplaced ///
    $controls [pw=surv_weight], abs(PROVID m3) vce(cluster DISTID#m3)
estimates store m4_1
mean govtcourts_fair [pw=surv_weight] if nondisplaced_pashtun == 1
global mean4_1 = r(table)[1,1]

reghdfe govtcourts_fair displaced_pashtun nonpashtun_displaced nonpashtun_nondisplaced ///
    $controls ln_insurgent_attacks_all [pw=surv_weight], abs(PROVID m3) vce(cluster DISTID#m3)
estimates store m4_2
mean govtcourts_fair [pw=surv_weight] if nondisplaced_pashtun == 1
global mean4_2 = r(table)[1,1]

* 3. Excluding nondisplaced Pashtuns in the North
reghdfe govtcourts_fair displaced_pashtun nonpashtun_displaced nonpashtun_nondisplaced ///
    $controls ln_insurgent_attacks_all [pw=surv_weight] if nondisplaced_pashtun_north == 0, ///
    abs(PROVID m3) vce(cluster DISTID#m3)
estimates store m4_3
mean govtcourts_fair [pw=surv_weight] if nondisplaced_pashtun_south == 1
global mean4_3 = r(table)[1,1]

* 4. Comparing Pashtuns in the North to all others
reghdfe govtcourts_fair all_pashtun_north $controls ln_insurgent_attacks_all [pw=surv_weight], ///
    abs(PROVID m3) vce(cluster DISTID#m3)
estimates store m4_4
mean govtcourts_fair [pw=surv_weight] if all_pashtun_north == 0
global mean4_4 = r(table)[1,1]

* 5. Only including Afghanis in the North
reghdfe govtcourts_fair displaced_pashtun nonpashtun_displaced $controls ///
    ln_insurgent_attacks_all [pw=surv_weight] if north_prov == 1, ///
    abs(PROVID m3) vce(cluster DISTID#m3)
estimates store m4_5
mean govtcourts_fair [pw=surv_weight] if north_prov == 1 & displaced_pashtun == 0 & nonpashtun_displaced == 0
global mean4_5 = r(table)[1,1]

* 6. Alternative measure
reghdfe govtcourts_fair displaced_pashtun2 nonpashtun_displaced nonpashtun_nondisplaced ///
    $controls ln_insurgent_attacks_all [pw=surv_weight], abs(PROVID m3) vce(cluster DISTID#m3)
estimates store m4_6
mean govtcourts_fair [pw=surv_weight] if displaced_pashtun2 == 0 & pashtun == 1
global mean4_6 = r(table)[1,1]


*------------------------------------------------------------
* Export legitimacy results for plotting
*------------------------------------------------------------
esttab m1_1 m1_2 m1_3 m1_4 m1_5 m1_6 ///
       m2_1 m2_2 m2_3 m2_4 m2_5 m2_6 ///
       m3_1 m3_2 m3_3 m3_4 m3_5 m3_6 ///
       m4_1 m4_2 m4_3 m4_4 m4_5 m4_6 ///
    using "replication/data/intermediate/FOGHORNresults_legit.csv", ///
    cells(b(fmt(3)) se(fmt(3))) ///
    label mlabels("provgovt1" "provgovt2" "provgovt3" ///
                  "provgovt4" "provgovt5" "provgovt6" ///
                  "jirga1"    "jirga2"    "jirga3"    ///
                  "jirga4"    "jirga5"    "jirga6"    ///
                  "centgovt1" "centgovt2" "centgovt3" ///
                  "centgovt4" "centgovt5" "centgovt6" ///
                  "judicial1" "judicial2" "judicial3" ///
                  "judicial4" "judicial5" "judicial6") csv

* Create a matrix with the predefined global values
matrix means = ($mean1_1, $mean1_2, $mean1_3, $mean1_4, $mean1_5, $mean1_6, ///
                 $mean2_1, $mean2_2, $mean2_3, $mean2_4, $mean2_5, $mean2_6, ///
                 $mean3_1, $mean3_2, $mean3_3, $mean3_4, $mean3_5, $mean3_6, ///
                 $mean4_1, $mean4_2, $mean4_3, $mean4_4, $mean4_5, $mean4_6)

* Add column names for clarity
matrix colnames means = provgovt1 provgovt2 provgovt3 provgovt4 provgovt5 provgovt6 ///
                       jirga1     jirga2     jirga3     jirga4     jirga5     jirga6 ///
                       centgovt1  centgovt2  centgovt3  centgovt4  centgovt5  centgovt6 ///
                       judicial1  judicial2  judicial3  judicial4  judicial5  judicial6

* Use esttab to export the matrix
esttab matrix(means) using "replication/data/intermediate/FOGHORNresults_legit_means.csv", replace

* Drop temporary macros
macro drop mean1_1
macro drop mean1_2
macro drop mean1_3
macro drop mean1_4
macro drop mean1_5
macro drop mean1_6
macro drop mean2_1
macro drop mean2_2
macro drop mean2_3
macro drop mean2_4
macro drop mean2_5
macro drop mean2_6
macro drop mean3_1
macro drop mean3_2
macro drop mean3_3
macro drop mean3_4
macro drop mean3_5
macro drop mean3_6
macro drop mean4_1
macro drop mean4_2
macro drop mean4_3
macro drop mean4_4
macro drop mean4_5
macro drop mean4_6

* Clear previous estimates
estimates clear


*------------------------------------------------------------
* Taliban Influence
*------------------------------------------------------------

* 1. Main result: more influence
reghdfe taliban_more_influence displaced_pashtun nonpashtun_displaced nonpashtun_nondisplaced ///
    $controls [pw=surv_weight], abs(PROVID m3) vce(cluster DISTID#m3)
estimates store m1_1
mean taliban_more_influence [pw=surv_weight] if nondisplaced_pashtun == 1
global mean1_1 = r(table)[1,1]

* 2. Include insurgent attacks (main text)
reghdfe taliban_more_influence displaced_pashtun nonpashtun_displaced nonpashtun_nondisplaced ///
    $controls ln_insurgent_attacks_all [pw=surv_weight], abs(PROVID m3) vce(cluster DISTID#m3)
estimates store m1_2
mean taliban_more_influence [pw=surv_weight] if nondisplaced_pashtun == 1
global mean1_2 = r(table)[1,1]

* 3. Excluding nondisplaced Pashtuns in the North
reghdfe taliban_more_influence displaced_pashtun nonpashtun_displaced nonpashtun_nondisplaced ///
    $controls ln_insurgent_attacks_all [pw=surv_weight] if nondisplaced_pashtun_north == 0, ///
    abs(PROVID m3) vce(cluster DISTID#m3)
estimates store m1_3
mean taliban_more_influence [pw=surv_weight] if nondisplaced_pashtun_south == 1
global mean1_3 = r(table)[1,1]

* 4. Comparing Pashtuns in the North to all others
reghdfe taliban_more_influence all_pashtun_north $controls ln_insurgent_attacks_all [pw=surv_weight], ///
    abs(PROVID m3) vce(cluster DISTID#m3)
estimates store m1_4
mean taliban_more_influence [pw=surv_weight] if all_pashtun_north == 0
global mean1_4 = r(table)[1,1]

* 5. Only including Afghanis in the North
reghdfe taliban_more_influence displaced_pashtun nonpashtun_displaced $controls ///
    ln_insurgent_attacks_all [pw=surv_weight] if north_prov == 1, ///
    abs(PROVID m3) vce(cluster DISTID#m3)
estimates store m1_5
mean taliban_more_influence [pw=surv_weight] if north_prov == 1 & displaced_pashtun == 0 & nonpashtun_displaced == 0
global mean1_5 = r(table)[1,1]

* 6. Alternative measure
reghdfe taliban_more_influence displaced_pashtun2 nonpashtun_displaced nonpashtun_nondisplaced ///
    $controls ln_insurgent_attacks_all [pw=surv_weight], abs(PROVID m3) vce(cluster DISTID#m3)
estimates store m1_6
mean taliban_more_influence [pw=surv_weight] if displaced_pashtun2 == 0 & pashtun == 1
global mean1_6 = r(table)[1,1]


*------------------------------------------------------------
* Community relation with the Taliban
*------------------------------------------------------------
reghdfe taliban_communityrelation displaced_pashtun nonpashtun_displaced nonpashtun_nondisplaced ///
    $controls [pw=surv_weight], abs(PROVID m3) vce(cluster DISTID#m3)
estimates store m2_1
mean taliban_communityrelation [pw=surv_weight] if nondisplaced_pashtun == 1
global mean2_1 = r(table)[1,1]

reghdfe taliban_communityrelation displaced_pashtun nonpashtun_displaced nonpashtun_nondisplaced ///
    $controls ln_insurgent_attacks_all [pw=surv_weight], abs(PROVID m3) vce(cluster DISTID#m3)
estimates store m2_2
mean taliban_communityrelation [pw=surv_weight] if nondisplaced_pashtun == 1
global mean2_2 = r(table)[1,1]

* 3. Excluding nondisplaced Pashtuns in the North
reghdfe taliban_communityrelation displaced_pashtun nonpashtun_displaced nonpashtun_nondisplaced ///
    $controls ln_insurgent_attacks_all [pw=surv_weight] if nondisplaced_pashtun_north == 0, ///
    abs(PROVID m3) vce(cluster DISTID#m3)
estimates store m2_3
mean taliban_communityrelation [pw=surv_weight] if nondisplaced_pashtun_south == 1
global mean2_3 = r(table)[1,1]

* 4. Comparing Pashtuns in the North to all others
reghdfe taliban_communityrelation all_pashtun_north $controls ln_insurgent_attacks_all [pw=surv_weight], ///
    abs(PROVID m3) vce(cluster DISTID#m3)
estimates store m2_4
mean taliban_communityrelation [pw=surv_weight] if all_pashtun_north == 0
global mean2_4 = r(table)[1,1]

* 5. Only including Afghanis in the North
reghdfe taliban_communityrelation displaced_pashtun nonpashtun_displaced $controls ///
    ln_insurgent_attacks_all [pw=surv_weight] if north_prov == 1, ///
    abs(PROVID m3) vce(cluster DISTID#m3)
estimates store m2_5
mean taliban_communityrelation [pw=surv_weight] if north_prov == 1 & displaced_pashtun == 0 & nonpashtun_displaced == 0
global mean2_5 = r(table)[1,1]

* 6. Alternative measure
reghdfe taliban_communityrelation displaced_pashtun2 nonpashtun_displaced nonpashtun_nondisplaced ///
    $controls ln_insurgent_attacks_all [pw=surv_weight], abs(PROVID m3) vce(cluster DISTID#m3)
estimates store m2_6
mean taliban_communityrelation [pw=surv_weight] if displaced_pashtun2 == 0 & pashtun == 1
global mean2_6 = r(table)[1,1]


*------------------------------------------------------------
* Trust in the Taliban
*------------------------------------------------------------
reghdfe trust_taliban displaced_pashtun nonpashtun_displaced nonpashtun_nondisplaced ///
    $controls [pw=surv_weight], abs(PROVID m3) vce(cluster DISTID#m3)
estimates store m3_1
mean trust_taliban [pw=surv_weight] if nondisplaced_pashtun == 1
global mean3_1 = r(table)[1,1]

reghdfe trust_taliban displaced_pashtun nonpashtun_displaced nonpashtun_nondisplaced ///
    $controls ln_insurgent_attacks_all [pw=surv_weight], abs(PROVID m3) vce(cluster DISTID#m3)
estimates store m3_2
mean trust_taliban [pw=surv_weight] if nondisplaced_pashtun == 1
global mean3_2 = r(table)[1,1]

* 3. Excluding nondisplaced Pashtuns in the North
reghdfe trust_taliban displaced_pashtun nonpashtun_displaced nonpashtun_nondisplaced ///
    $controls ln_insurgent_attacks_all [pw=surv_weight] if nondisplaced_pashtun_north == 0, ///
    abs(PROVID m3) vce(cluster DISTID#m3)
estimates store m3_3
mean trust_taliban [pw=surv_weight] if nondisplaced_pashtun_south == 1
global mean3_3 = r(table)[1,1]

* 4. Comparing Pashtuns in the North to all others
reghdfe trust_taliban all_pashtun_north $controls ln_insurgent_attacks_all [pw=surv_weight], ///
    abs(PROVID m3) vce(cluster DISTID#m3)
estimates store m3_4
mean trust_taliban [pw=surv_weight] if all_pashtun_north == 0
global mean3_4 = r(table)[1,1]

* 5. Only including Afghanis in the North
reghdfe trust_taliban displaced_pashtun nonpashtun_displaced $controls ///
    ln_insurgent_attacks_all [pw=surv_weight] if north_prov == 1, ///
    abs(PROVID m3) vce(cluster DISTID#m3)
estimates store m3_5
mean trust_taliban [pw=surv_weight] if north_prov == 1 & displaced_pashtun == 0 & nonpashtun_displaced == 0
global mean3_5 = r(table)[1,1]

* 6. Alternative measure
reghdfe trust_taliban displaced_pashtun2 nonpashtun_displaced nonpashtun_nondisplaced ///
    $controls ln_insurgent_attacks_all [pw=surv_weight], abs(PROVID m3) vce(cluster DISTID#m3)
estimates store m3_6
mean trust_taliban [pw=surv_weight] if displaced_pashtun2 == 0 & pashtun == 1
global mean3_6 = r(table)[1,1]


*------------------------------------------------------------
* Perception of fair courts under the Taliban
*------------------------------------------------------------
reghdfe taliban_courts_fair displaced_pashtun nonpashtun_displaced nonpashtun_nondisplaced ///
    $controls [pw=surv_weight], abs(PROVID m3) vce(cluster DISTID#m3)
estimates store m4_1
mean taliban_courts_fair [pw=surv_weight] if nondisplaced_pashtun == 1
global mean4_1 = r(table)[1,1]

reghdfe taliban_courts_fair displaced_pashtun nonpashtun_displaced nonpashtun_nondisplaced ///
    $controls ln_insurgent_attacks_all [pw=surv_weight], abs(PROVID m3) vce(cluster DISTID#m3)
estimates store m4_2
mean taliban_courts_fair [pw=surv_weight] if nondisplaced_pashtun == 1
global mean4_2 = r(table)[1,1]

* 3. Excluding nondisplaced Pashtuns in the North
reghdfe taliban_courts_fair displaced_pashtun nonpashtun_displaced nonpashtun_nondisplaced ///
    $controls ln_insurgent_attacks_all [pw=surv_weight] if nondisplaced_pashtun_north == 0, ///
    abs(PROVID m3) vce(cluster DISTID#m3)
estimates store m4_3
mean taliban_courts_fair [pw=surv_weight] if nondisplaced_pashtun_south == 1
global mean4_3 = r(table)[1,1]

* 4. Comparing Pashtuns in the North to all others
reghdfe taliban_courts_fair all_pashtun_north $controls ln_insurgent_attacks_all [pw=surv_weight], ///
    abs(PROVID m3) vce(cluster DISTID#m3)
estimates store m4_4
mean taliban_courts_fair [pw=surv_weight] if all_pashtun_north == 0
global mean4_4 = r(table)[1,1]

* 5. Only including Afghanis in the North
reghdfe taliban_courts_fair displaced_pashtun nonpashtun_displaced $controls ///
    ln_insurgent_attacks_all [pw=surv_weight] if north_prov == 1, ///
    abs(PROVID m3) vce(cluster DISTID#m3)
estimates store m4_5
mean taliban_courts_fair [pw=surv_weight] if north_prov == 1 & displaced_pashtun == 0 & nonpashtun_displaced == 0
global mean4_5 = r(table)[1,1]

* 6. Alternative measure
reghdfe taliban_courts_fair displaced_pashtun2 nonpashtun_displaced nonpashtun_nondisplaced ///
    $controls ln_insurgent_attacks_all [pw=surv_weight], abs(PROVID m3) vce(cluster DISTID#m3)
estimates store m4_6
mean taliban_courts_fair [pw=surv_weight] if displaced_pashtun2 == 0 & pashtun == 1
global mean4_6 = r(table)[1,1]


*------------------------------------------------------------
* Export Taliban views results for plotting
*------------------------------------------------------------
esttab m1_1 m1_2 m1_3 m1_4 m1_5 m1_6 ///
       m2_1 m2_2 m2_3 m2_4 m2_5 m2_6 ///
       m3_1 m3_2 m3_3 m3_4 m3_5 m3_6 ///
       m4_1 m4_2 m4_3 m4_4 m4_5 m4_6 ///
    using "replication/data/intermediate/FOGHORNresults_taliban.csv", ///
    cells(b(fmt(3)) se(fmt(3))) ///
    label mlabels("more_influence1" "more_influence2" "more_influence3" ///
                  "more_influence4" "more_influence5" "more_influence6" ///
                  "communityrelation1" "communityrelation2" "communityrelation3" ///
                  "communityrelation4" "communityrelation5" "communityrelation6" ///
                  "trust1" "trust2" "trust3" "trust4" "trust5" "trust6" ///
                  "courts_fair1" "courts_fair2" "courts_fair3" "courts_fair4" ///
                  "courts_fair5" "courts_fair6") csv

* Create a matrix with the global values
matrix means = ($mean1_1, $mean1_2, $mean1_3, $mean1_4, $mean1_5, $mean1_6, ///
                 $mean2_1, $mean2_2, $mean2_3, $mean2_4, $mean2_5, $mean2_6, ///
                 $mean3_1, $mean3_2, $mean3_3, $mean3_4, $mean3_5, $mean3_6, ///
                 $mean4_1, $mean4_2, $mean4_3, $mean4_4, $mean4_5, $mean4_6)

* Add column names for clarity
matrix colnames means = more_influence1 more_influence2 more_influence3 ///
                       more_influence4 more_influence5 more_influence6 ///
                       communityrelation1 communityrelation2 communityrelation3 ///
                       communityrelation4 communityrelation5 communityrelation6 ///
                       trust1 trust2 trust3 trust4 trust5 trust6 ///
                       courts_fair1 courts_fair2 courts_fair3 ///
                       courts_fair4 courts_fair5 courts_fair6

* Export the matrix of means
esttab matrix(means) using "replication/data/intermediate/FOGHORNresults_taliban_means.csv", replace

* Drop temporary macros
macro drop mean1_1
macro drop mean1_2
macro drop mean1_3
macro drop mean1_4
macro drop mean1_5
macro drop mean1_6
macro drop mean2_1
macro drop mean2_2
macro drop mean2_3
macro drop mean2_4
macro drop mean2_5
macro drop mean2_6
macro drop mean3_1
macro drop mean3_2
macro drop mean3_3
macro drop mean3_4
macro drop mean3_5
macro drop mean3_6
macro drop mean4_1
macro drop mean4_2
macro drop mean4_3
macro drop mean4_4
macro drop mean4_5
macro drop mean4_6

* Clear previous estimates
estimates clear

*------------------------------------------------------------
* Fled for violence
*------------------------------------------------------------
reghdfe fledhome_polviol displaced_pashtun nonpashtun_displaced nonpashtun_nondisplaced ///
    $controls ln_insurgent_attacks_all [pw=surv_weight], abs(PROVID m3) vce(cluster DISTID#m3)
estimates store m1_2_allage
mean fledhome_polviol [pw=surv_weight] if nondisplaced_pashtun == 1
global mean1_2_allage = r(table)[1,1]

reghdfe fledhome_polviol displaced_pashtun nonpashtun_displaced nonpashtun_nondisplaced ///
    $controls ln_insurgent_attacks_all [pw=surv_weight] if age >= 50, ///
    abs(PROVID m3) vce(cluster DISTID#m3)
estimates store m1_2_old
mean fledhome_polviol [pw=surv_weight] if nondisplaced_pashtun == 1 & age >= 50
global mean1_2_old = r(table)[1,1]

reghdfe fledhome_polviol displaced_pashtun nonpashtun_displaced nonpashtun_nondisplaced ///
    $controls ln_insurgent_attacks_all [pw=surv_weight] if age < 50, ///
    abs(PROVID m3) vce(cluster DISTID#m3)
estimates store m1_2_young
mean fledhome_polviol [pw=surv_weight] if nondisplaced_pashtun == 1 & age < 50
global mean1_2_young = r(table)[1,1]


*------------------------------------------------------------
* Fled for jobs
*------------------------------------------------------------
reghdfe fledhome_jobs displaced_pashtun nonpashtun_displaced nonpashtun_nondisplaced ///
    $controls ln_insurgent_attacks_all [pw=surv_weight], abs(PROVID m3) vce(cluster DISTID#m3)
estimates store m2_2_allage
mean fledhome_polviol [pw=surv_weight] if nondisplaced_pashtun == 1
global mean2_2_allage = r(table)[1,1]

reghdfe fledhome_jobs displaced_pashtun nonpashtun_displaced nonpashtun_nondisplaced ///
    $controls ln_insurgent_attacks_all [pw=surv_weight] if age >= 50, ///
    abs(PROVID m3) vce(cluster DISTID#m3)
estimates store m2_2_old
mean fledhome_polviol [pw=surv_weight] if nondisplaced_pashtun == 1 & age >= 50
global mean2_2_old = r(table)[1,1]

reghdfe fledhome_jobs displaced_pashtun nonpashtun_displaced nonpashtun_nondisplaced ///
    $controls ln_insurgent_attacks_all [pw=surv_weight] if age < 50, ///
    abs(PROVID m3) vce(cluster DISTID#m3)
estimates store m2_2_young
mean fledhome_polviol [pw=surv_weight] if nondisplaced_pashtun == 1 & age < 50
global mean2_2_young = r(table)[1,1]


*------------------------------------------------------------
* Moved for any reason
*------------------------------------------------------------
reghdfe moved_anyreason displaced_pashtun nonpashtun_displaced nonpashtun_nondisplaced ///
    $controls ln_insurgent_attacks_all [pw=surv_weight], abs(PROVID m3) vce(cluster DISTID#m3)
estimates store m3_2_allage
mean moved_anyreason [pw=surv_weight] if nondisplaced_pashtun == 1
global mean3_2_allage = r(table)[1,1]

reghdfe moved_anyreason displaced_pashtun nonpashtun_displaced nonpashtun_nondisplaced ///
    $controls ln_insurgent_attacks_all [pw=surv_weight] if age >= 50, ///
    abs(PROVID m3) vce(cluster DISTID#m3)
estimates store m3_2_old
mean moved_anyreason [pw=surv_weight] if nondisplaced_pashtun == 1 & age >= 50
global mean3_2_old = r(table)[1,1]

reghdfe moved_anyreason displaced_pashtun nonpashtun_displaced nonpashtun_nondisplaced ///
    $controls ln_insurgent_attacks_all [pw=surv_weight] if age < 50, ///
    abs(PROVID m3) vce(cluster DISTID#m3)
estimates store m3_2_young
mean moved_anyreason [pw=surv_weight] if nondisplaced_pashtun == 1 & age < 50
global mean3_2_young = r(table)[1,1]


*------------------------------------------------------------
* Community safety
*------------------------------------------------------------
reghdfe community_safety displaced_pashtun nonpashtun_displaced nonpashtun_nondisplaced ///
    $controls ln_insurgent_attacks_all [pw=surv_weight], abs(PROVID m3) vce(cluster DISTID#m3)
estimates store m4_2_allage
mean community_safety [pw=surv_weight] if nondisplaced_pashtun == 1
global mean4_2_allage = r(table)[1,1]

reghdfe community_safety displaced_pashtun nonpashtun_displaced nonpashtun_nondisplaced ///
    $controls ln_insurgent_attacks_all [pw=surv_weight] if age >= 50, ///
    abs(PROVID m3) vce(cluster DISTID#m3)
estimates store m4_2_old
mean community_safety [pw=surv_weight] if nondisplaced_pashtun == 1 & age >= 50
global mean4_2_old = r(table)[1,1]

reghdfe community_safety displaced_pashtun nonpashtun_displaced nonpashtun_nondisplaced ///
    $controls ln_insurgent_attacks_all [pw=surv_weight] if age < 50, ///
    abs(PROVID m3) vce(cluster DISTID#m3)
estimates store m4_2_young
mean community_safety [pw=surv_weight] if nondisplaced_pashtun == 1 & age < 50
global mean4_2_young = r(table)[1,1]


*------------------------------------------------------------
* Export fled‐home results for plotting
*------------------------------------------------------------
esttab m1_2_allage m1_2_old m1_2_young ///
       m2_2_allage m2_2_old m2_2_young ///
       m3_2_allage m3_2_old m3_2_young ///
       m4_2_allage m4_2_old m4_2_young ///
    using "replication/data/intermediate/FOGHORNresults_fledhome.csv", ///
    cells(b(fmt(3)) se(fmt(3))) ///
    label mlabels("polviol2allage" "polviol2old" "polviol2young" ///
                  "jobs2allage"    "jobs2old"    "jobs2young"    ///
                  "anyreason2allage" "anyreason2old" "anyreason2young" ///
                  "community_safety2allage" "community_safety2old" "community_safety2young") csv

* Create a matrix with global values
matrix means = ($mean1_2_allage, $mean1_2_old, $mean1_2_young, ///
                $mean2_2_allage, $mean2_2_old, $mean2_2_young, ///
                $mean3_2_allage, $mean3_2_old, $mean3_2_young, ///
                $mean4_2_allage, $mean4_2_old, $mean4_2_young)

* Add column names for clarity
matrix colnames means = polviol2allage polviol2old polviol2young ///
                       jobs2allage    jobs2old    jobs2young    ///
                       anyreason2allage anyreason2old anyreason2young ///
                       community_safety2allage community_safety2old community_safety2young

* Export the matrix of means
esttab matrix(means) using "replication/data/intermediate/FOGHORNresults_fledhome_means.csv", replace

* Drop temporary macros
macro drop mean1_2_allage
macro drop mean1_2_old
macro drop mean1_2_young

macro drop mean2_2_allage
macro drop mean2_2_old
macro drop mean2_2_young

macro drop mean3_2_allage
macro drop mean3_2_old
macro drop mean3_2_young

macro drop mean4_2_allage
macro drop mean4_2_old
macro drop mean4_2_young


* Clear previous estimates
estimates clear


*------------------------------------------------------------
* Section 2: Displaying Regression Tables
*------------------------------------------------------------


*------------------------------------------------------------
* Appendix 1.2: Alternative Measure of the Displaced Pashtuns
*------------------------------------------------------------

* Table A2: Cross-tabulation of the Original and Alternative Measures 
*          of Displaced Pashtuns (FOGHORN)
*   Pearson correlation stated in text in Appendix 1.2 Alternative Measure
corr displaced_pashtun displaced_pashtun2
* Table A2
tabulate displaced_pashtun displaced_pashtun2


*------------------------------------------------------------
* Appendix 2: Details on the Survey Data
*------------------------------------------------------------

* Table A4: Pairwise Correlations between Income per capita, Hunger, and Possess 
*          with Significance Indicated by * (p < 0.05) and Corresponding Sample Sizes
pwcorr income_pc hunger possess, sig star(0.05) obs


*------------------------------------------------------------
* Appendix 5: Tables of the Results 
*------------------------------------------------------------

* Table A18: Stability and Safety in Displaced Communities
estimates clear

foreach outcome in fledhome_polviol fledhome_jobs moved_anyreason community_safety {
    foreach insurr in "" "ln_insurgent_attacks_all" {
        qui eststo: reghdfe `outcome' displaced_pashtun nonpashtun_displaced nonpashtun_nondisplaced ///
            $controls `insurr' [pw=surv_weight], abs(PROVID m3) vce(cluster DISTID#m3)
        estadd local provfe = "Yes"
        estadd local wavefe = "Yes"
    }
}
esttab, ///
    booktabs ///
    stats(provfe wavefe N, label("Province Fixed Effects?" "Survey Wave Fixed Effect?" "N")) ///
    varlabels(_cons Constant) ///
    starlevels(* 0.1 ** 0.05) ///
    mtitles($m1m2 $m1m2 $m1m2 $m1m2) ///
    nonumbers ///
    mgroups("Feld Home Political Violence?" "Fled Home Ecomomic?" "Fled Home Any Reason?" "Feel Safe in Community?", ///
            pattern(1 0 1 0 1 0 1 0)) ///
    note("Robust standard errors clustered by districts and survey waves in parentheses.") ///
    se(2) b(3)
	

* Table A20: Effectiveness of Government and Institutions
estimates clear
foreach outcome in prov_council_effective cent_govt_effective ulama_effective locgovt_communityrelation {
    foreach insurr in "" "ln_insurgent_attacks_all" {
        qui eststo: reghdfe `outcome' displaced_pashtun nonpashtun_displaced nonpashtun_nondisplaced ///
            $controls `insurr' [pw=surv_weight], abs(PROVID m3) vce(cluster DISTID#m3)
        estadd local provfe = "Yes"
        estadd local wavefe = "Yes"
    }
}
esttab, booktabs ///
    stats(provfe wavefe N, label("Province Fixed Effects?" "Survey Wave Fixed Effect?" "N")) ///
    varlabels(_cons Constant) ///
    starlevels(* 0.1 ** 0.05) ///
    mtitles($m1m2 $m1m2 $m1m2 $m1m2) ///
    nonumbers ///
    mgroups("Provincial Government Effective?" "Central Government Effective?" ///
            "Govt Ulama Council Effective?" "Local Govt. Community Relations Good?", ///
            pattern(1 0 1 0 1 0 1 0)) ///
    note("Robust standard errors clustered by districts and survey waves in parentheses.") ///
    se(2) b(3)


* Table A21: Legitimacy of Government and Institutions
estimates clear
foreach outcome in provgovt_legit jirga_legit centgovt_legit govtcourts_fair {
    foreach insurr in "" "ln_insurgent_attacks_all" {
        qui eststo: reghdfe `outcome' displaced_pashtun nonpashtun_displaced nonpashtun_nondisplaced ///
            $controls `insurr' [pw=surv_weight], abs(PROVID m3) vce(cluster DISTID#m3)
        estadd local provfe = "Yes"
        estadd local wavefe = "Yes"
    }
}
esttab, booktabs ///
    stats(provfe wavefe N, label("Province Fixed Effects?" "Survey Wave Fixed Effect?" "N")) ///
    varlabels(_cons Constant) ///
    starlevels(* 0.1 ** 0.05) ///
    mtitles($m1m2 $m1m2 $m1m2 $m1m2) ///
    nonumbers ///
    mgroups("Provincial Government Legitimate?" "Wolesi Jirga Legitimate?" ///
            "Central Government Legitimate?" "Government Courts Fair?", ///
            pattern(1 0 1 0 1 0 1 0)) ///
    note("Robust standard errors clustered by districts and survey waves in parentheses.") ///
    se(2) b(3)


* Table A22: Views of the Taliban and its Institutions
estimates clear
foreach outcome in taliban_more_influence taliban_communityrelation trust_taliban taliban_courts_fair {
    foreach insurr in "" "ln_insurgent_attacks_all" {
        qui eststo: reghdfe `outcome' displaced_pashtun nonpashtun_displaced nonpashtun_nondisplaced ///
            $controls `insurr' [pw=surv_weight], abs(PROVID m3) vce(cluster DISTID#m3)
        estadd local provfe = "Yes"
        estadd local wavefe = "Yes"
    }
}
esttab, booktabs ///
    stats(provfe wavefe N, label("Province Fixed Effects?" "Survey Wave Fixed Effect?" "N")) ///
    varlabels(_cons Constant) ///
    starlevels(* 0.1 ** 0.05) ///
    mtitles($m1m2 $m1m2 $m1m2 $m1m2) ///
    nonumbers ///
    mgroups("Does the Taliban Have More Influence than the Government?" "Are Taliban Relations with Your Community Favorable?" ///
            "Do You Trust Statements Made by the Taliban?" "Are Taliban Courts Fair?", ///
            pattern(1 0 1 0 1 0 1 0)) ///
    note("Robust standard errors clustered by districts and survey waves in parentheses.") ///
    se(2) b(3)


*------------------------------------------------------------
* Appendix 4.3.1: State Presence and Investment in Infrastructure
*------------------------------------------------------------
* Table A10: Effectiveness of Government and Institutions
estimates clear
foreach outcome in prov_council_effective cent_govt_effective ulama_effective locgovt_communityrelation {
    foreach insurr in "" "ln_insurgent_attacks_all" {
        qui eststo: reghdfe `outcome' displaced_pashtun nonpashtun_displaced nonpashtun_nondisplaced ///
            nightlightlog $controls `insurr' [pw=surv_weight], abs(PROVID m3) vce(cluster DISTID#m3)
        estadd local provfe = "Yes"
        estadd local wavefe = "Yes"
    }
}
esttab, booktabs ///
    stats(provfe wavefe N, label("Province Fixed Effects?" "Survey Wave Fixed Effect?" "N")) ///
    varlabels(_cons Constant) ///
    starlevels(* 0.1 ** 0.05) ///
    mtitles($m1m2 $m1m2 $m1m2 $m1m2) ///
    nonumbers ///
    mgroups("Provincial Government Effective?" "Central Government Effective?" ///
            "Govt Ulama Council Effective?" "Local Govt. Community Relations Good?", ///
            pattern(1 0 1 0 1 0 1 0)) ///
    note("Robust standard errors clustered by districts and survey waves in parentheses.") ///
    se(2) b(3)


* Table A11: Legitimacy of Government and Institutions
estimates clear
foreach outcome in provgovt_legit jirga_legit centgovt_legit govtcourts_fair {
    foreach insurr in "" "ln_insurgent_attacks_all" {
        qui eststo: reghdfe `outcome' displaced_pashtun nonpashtun_displaced nonpashtun_nondisplaced ///
            nightlightlog $controls `insurr' [pw=surv_weight], abs(PROVID m3) vce(cluster DISTID#m3)
        estadd local provfe = "Yes"
        estadd local wavefe = "Yes"
    }
}
esttab, booktabs ///
    stats(provfe wavefe N, label("Province Fixed Effects?" "Survey Wave Fixed Effect?" "N")) ///
    varlabels(_cons Constant) ///
    starlevels(* 0.1 ** 0.05) ///
    mtitles($m1m2 $m1m2 $m1m2 $m1m2) ///
    nonumbers ///
    mgroups("Provincial Government Legitimate?" "Wolesi Jirga Legitimate?" ///
            "Central Government Legitimate?" "Government Courts Fair?", ///
            pattern(1 0 1 0 1 0 1 0)) ///
    note("Robust standard errors clustered by districts and survey waves in parentheses.") ///
    se(2) b(3)


* Table A12: National and Local Police Presence 	
estimates clear
reghdfe ANP_presence ///
    displaced_pashtun nonpashtun_displaced nonpashtun_nondisplaced ///
    $controls ln_insurgent_attacks_all [pw=surv_weight], ///
    abs(PROVID m3) vce(cluster DISTID#m3)
estimates store m1
reghdfe ALP_presence ///
    displaced_pashtun nonpashtun_displaced nonpashtun_nondisplaced ///
    $controls ln_insurgent_attacks_all [pw=surv_weight], ///
    abs(PROVID m3) vce(cluster DISTID#m3)
estimates store m2
esttab m1 m2, ///
    booktabs ///
    stats(provfe wavefe N, ///
      label("Province Fixed Effects?" "Survey Wave Fixed Effect?" "N")) ///
    varlabels(_cons Constant) ///
    starlevels(* 0.1 ** 0.05) ///
    mtitles($m1m2) ///
    nonumbers ///
    mgroups("Police Presence", pattern(1 0)) ///
    note("Robust standard errors clustered by districts and survey waves in parentheses.") ///
    se(2) b(3)
	
	
*------------------------------------------------------------
* Appendix 4.4: Soviet and US Influence
*------------------------------------------------------------
* Table A15: Effectiveness of Government and Institutions
estimates clear
foreach outcome in prov_council_effective cent_govt_effective ulama_effective locgovt_communityrelation {
    foreach insurr in "" "ln_insurgent_attacks_all" {
        qui eststo: reghdfe `outcome' displaced_pashtun nonpashtun_displaced nonpashtun_nondisplaced ///
            soviet ln_dist2PAKborder $controls `insurr' [pw=surv_weight], abs(PROVID m3) vce(cluster DISTID#m3)
        estadd local provfe = "Yes"
        estadd local wavefe = "Yes"
    }
}
esttab, ///
    booktabs ///
    stats(provfe wavefe N, ///
          label("Province Fixed Effects?" "Survey Wave Fixed Effect?" "N")) ///
    varlabels(_cons Constant) ///
    starlevels(* 0.1 ** 0.05) ///
    mtitles($m1m2 $m1m2 $m1m2 $m1m2) ///
    nonumbers ///
    mgroups("Provincial Government Effective?" "Central Government Effective?" ///
            "Govt Ulama Council Effective?" "Local Govt. Community Relations Good?", ///
            pattern(1 0 1 0 1 0 1 0)) ///
    note("Robust standard errors clustered by districts and survey waves in parentheses.") ///
    se(2) b(3)


* Table A16: Legitimacy of Government and Institutions
estimates clear
foreach outcome in provgovt_legit jirga_legit centgovt_legit govtcourts_fair {
    foreach insurr in "" "ln_insurgent_attacks_all" {
        qui eststo: reghdfe `outcome' displaced_pashtun nonpashtun_displaced nonpashtun_nondisplaced ///
            soviet ln_dist2PAKborder $controls `insurr' [pw=surv_weight], abs(PROVID m3) vce(cluster DISTID#m3)
        estadd local provfe = "Yes"
        estadd local wavefe = "Yes"
    }
}
esttab, ///
    booktabs ///
    stats(provfe wavefe N, ///
          label("Province Fixed Effects?" "Survey Wave Fixed Effect?" "N")) ///
    varlabels(_cons Constant) ///
    starlevels(* 0.1 ** 0.05) ///
    mtitles($m1m2 $m1m2 $m1m2 $m1m2) ///
    nonumbers ///
    mgroups("Provincial Government Legitimate?" "Wolesi Jirga Legitimate?" ///
            "Central Government Legitimate?" "Government Courts Fair?", ///
            pattern(1 0 1 0 1 0 1 0)) ///
    note("Robust standard errors clustered by districts and survey waves in parentheses.") ///
    se(2) b(3)


* Table A17: Views of the Taliban and its Institutions
estimates clear
foreach outcome in taliban_more_influence taliban_communityrelation trust_taliban taliban_courts_fair {
    foreach insurr in "" "ln_insurgent_attacks_all" {
        qui eststo: reghdfe `outcome' displaced_pashtun nonpashtun_displaced nonpashtun_nondisplaced ///
            soviet ln_dist2PAKborder $controls `insurr' [pw=surv_weight], abs(PROVID m3) vce(cluster DISTID#m3)
        estadd local provfe = "Yes"
        estadd local wavefe = "Yes"
    }
}
esttab, ///
    booktabs ///
    stats(provfe wavefe N, ///
          label("Province Fixed Effects?" "Survey Wave Fixed Effect?" "N")) ///
    varlabels(_cons Constant) ///
    starlevels(* 0.1 ** 0.05) ///
    mtitles($m1m2 $m1m2 $m1m2 $m1m2) ///
    nonumbers ///
    mgroups("Does the Taliban Have More Influence than the Government?" ///
            "Are Taliban Relations with Your Community Favorable?" ///
            "Do You Trust Statements Made by the Taliban?" ///
            "Are Taliban Courts Fair?", ///
            pattern(1 0 1 0 1 0 1 0)) ///
    note("Robust standard errors clustered by districts and survey waves in parentheses.") ///
    se(2) b(3)
	

estimates clear

*------------------------------------------------------------
* Appendix 3.2 Different Pashtun Tribes – Durani and Ghilzai
*------------------------------------------------------------
gen displaced_durani = displaced_pashtun == 1 & displaced_region == "Durani"
gen displaced_ghil   = displaced_pashtun == 1 & displaced_region == "Ghilzai"


* Table A7: Effectiveness of Government and Institutions
reghdfe prov_council_effective displaced_durani displaced_ghil nonpashtun_displaced nonpashtun_nondisplaced ///
    $controls ln_insurgent_attacks_all [pw=surv_weight], abs(PROVID m3) vce(cluster DISTID#m3)
estimates store m1_1

reghdfe prov_council_effective displaced_durani displaced_ghil nonpashtun_displaced ///
    $controls ln_insurgent_attacks_all [pw=surv_weight] if north_prov == 1, abs(PROVID m3) vce(cluster DISTID#m3)
estimates store m1_2

reghdfe cent_govt_effective displaced_durani displaced_ghil nonpashtun_displaced nonpashtun_nondisplaced ///
    $controls ln_insurgent_attacks_all [pw=surv_weight], abs(PROVID m3) vce(cluster DISTID#m3)
estimates store m2_1

reghdfe cent_govt_effective displaced_durani displaced_ghil nonpashtun_displaced ///
    $controls ln_insurgent_attacks_all [pw=surv_weight] if north_prov == 1, abs(PROVID m3) vce(cluster DISTID#m3)
estimates store m2_2

reghdfe ulama_effective displaced_durani displaced_ghil nonpashtun_displaced nonpashtun_nondisplaced ///
    $controls ln_insurgent_attacks_all [pw=surv_weight], abs(PROVID m3) vce(cluster DISTID#m3)
estimates store m3_1

reghdfe ulama_effective displaced_durani displaced_ghil nonpashtun_displaced ///
    $controls ln_insurgent_attacks_all [pw=surv_weight] if north_prov == 1, abs(PROVID m3) vce(cluster DISTID#m3)
estimates store m3_2

reghdfe locgovt_communityrelation displaced_durani displaced_ghil nonpashtun_displaced nonpashtun_nondisplaced ///
    $controls ln_insurgent_attacks_all [pw=surv_weight], abs(PROVID m3) vce(cluster DISTID#m3)
estimates store m4_1

reghdfe locgovt_communityrelation displaced_durani displaced_ghil nonpashtun_displaced ///
    $controls ln_insurgent_attacks_all [pw=surv_weight] if north_prov == 1, abs(PROVID m3) vce(cluster DISTID#m3)
estimates store m4_2

esttab m1_1 m1_2 m2_1 m2_2 m3_1 m3_2 m4_1 m4_2, ///
    booktabs stats(provfe wavefe N, label("Province Fixed Effects?" "Survey Wave Fixed Effect?" "N")) ///
    varlabels(_cons Constant) starlevels(* 0.1 ** 0.05) ///
    mtitles($m1m2 $m1m2 $m1m2 $m1m2) nonumbers ///
    mgroups("Provincial Government Effective?" "Central Government Effective?" ///
            "Govt Ulama Council Effective?" "Local Govt. Community Relations Good?", ///
            pattern(1 0 1 0 1 0 1 0)) ///
    note("Robust standard errors clustered by districts and survey waves in parentheses.") ///
    se(2) b(3)


* Table A8: Legitimacy of Government and Institutions
reghdfe provgovt_legit displaced_durani displaced_ghil nonpashtun_displaced nonpashtun_nondisplaced ///
    $controls ln_insurgent_attacks_all [pw=surv_weight], abs(PROVID m3) vce(cluster DISTID#m3)
estimates store m1_1

reghdfe provgovt_legit displaced_durani displaced_ghil nonpashtun_displaced ///
    $controls ln_insurgent_attacks_all [pw=surv_weight] if north_prov == 1, abs(PROVID m3) vce(cluster DISTID#m3)
estimates store m1_2

reghdfe jirga_legit displaced_durani displaced_ghil nonpashtun_displaced nonpashtun_nondisplaced ///
    $controls ln_insurgent_attacks_all [pw=surv_weight], abs(PROVID m3) vce(cluster DISTID#m3)
estimates store m2_1

reghdfe jirga_legit displaced_durani displaced_ghil nonpashtun_displaced ///
    $controls ln_insurgent_attacks_all [pw=surv_weight] if north_prov == 1, abs(PROVID m3) vce(cluster DISTID#m3)
estimates store m2_2

reghdfe centgovt_legit displaced_durani displaced_ghil nonpashtun_displaced nonpashtun_nondisplaced ///
    $controls ln_insurgent_attacks_all [pw=surv_weight], abs(PROVID m3) vce(cluster DISTID#m3)
estimates store m3_1

reghdfe centgovt_legit displaced_durani displaced_ghil nonpashtun_displaced ///
    $controls ln_insurgent_attacks_all [pw=surv_weight] if north_prov == 1, abs(PROVID m3) vce(cluster DISTID#m3)
estimates store m3_2

reghdfe govtcourts_fair displaced_durani displaced_ghil nonpashtun_displaced nonpashtun_nondisplaced ///
    $controls ln_insurgent_attacks_all [pw=surv_weight], abs(PROVID m3) vce(cluster DISTID#m3)
estimates store m4_1

reghdfe govtcourts_fair displaced_durani displaced_ghil nonpashtun_displaced ///
    $controls ln_insurgent_attacks_all [pw=surv_weight] if north_prov == 1, abs(PROVID m3) vce(cluster DISTID#m3)
estimates store m4_2

esttab m1_1 m1_2 m2_1 m2_2 m3_1 m3_2 m4_1 m4_2, ///
    booktabs stats(provfe wavefe N, label("Province Fixed Effects?" "Survey Wave Fixed Effect?" "N")) ///
    varlabels(_cons Constant) starlevels(* 0.1 ** 0.05) ///
    mtitles($m1m2 $m1m2 $m1m2 $m1m2) nonumbers ///
    mgroups("Provincial Government Legitimate?" "Wolesi Jirga Legitimate?" ///
            "Central Government Legitimate?" "Government Courts Fair?", ///
            pattern(1 0 1 0 1 0 1 0)) ///
    note("Robust standard errors clustered by districts and survey waves in parentheses.") ///
    se(2) b(3)


* Table A9: Views of the Taliban and its Institutions
reghdfe taliban_more_influence displaced_durani displaced_ghil nonpashtun_displaced nonpashtun_nondisplaced ///
    $controls ln_insurgent_attacks_all [pw=surv_weight], abs(PROVID m3) vce(cluster DISTID#m3)
estimates store m1_1

reghdfe taliban_more_influence displaced_durani displaced_ghil nonpashtun_displaced ///
    $controls ln_insurgent_attacks_all [pw=surv_weight] if north_prov == 1, abs(PROVID m3) vce(cluster DISTID#m3)
estimates store m1_2

reghdfe taliban_communityrelation displaced_durani displaced_ghil nonpashtun_displaced nonpashtun_nondisplaced ///
    $controls ln_insurgent_attacks_all [pw=surv_weight], abs(PROVID m3) vce(cluster DISTID#m3)
estimates store m2_1

reghdfe taliban_communityrelation displaced_durani displaced_ghil nonpashtun_displaced ///
    $controls ln_insurgent_attacks_all [pw=surv_weight] if north_prov == 1, abs(PROVID m3) vce(cluster DISTID#m3)
estimates store m2_2

reghdfe trust_taliban displaced_durani displaced_ghil nonpashtun_displaced nonpashtun_nondisplaced ///
    $controls ln_insurgent_attacks_all [pw=surv_weight], abs(PROVID m3) vce(cluster DISTID#m3)
estimates store m3_1

reghdfe trust_taliban displaced_durani displaced_ghil nonpashtun_displaced ///
    $controls ln_insurgent_attacks_all [pw=surv_weight] if north_prov == 1, abs(PROVID m3) vce(cluster DISTID#m3)
estimates store m3_2

reghdfe taliban_courts_fair displaced_durani displaced_ghil nonpashtun_displaced nonpashtun_nondisplaced ///
    $controls ln_insurgent_attacks_all [pw=surv_weight], abs(PROVID m3) vce(cluster DISTID#m3)
estimates store m4_1

reghdfe taliban_courts_fair displaced_durani displaced_ghil nonpashtun_displaced ///
    $controls ln_insurgent_attacks_all [pw=surv_weight] if north_prov == 1, abs(PROVID m3) vce(cluster DISTID#m3)
estimates store m4_2

esttab m1_1 m1_2 m2_1 m2_2 m3_1 m3_2 m4_1 m4_2, ///
    booktabs stats(provfe wavefe N, label("Province Fixed Effects?" "Survey Wave Fixed Effect?" "N")) ///
    varlabels(_cons Constant) starlevels(* 0.1 ** 0.05) ///
    mtitles($m1m2 $m1m2 $m1m2 $m1m2) nonumbers ///
    mgroups("Does the Taliban Have More Influence than the Government?" ///
            "Are Taliban Relations with Your Community Favorable?" ///
            "Do You Trust Statements Made by the Taliban?" ///
            "Are Taliban Courts Fair?", pattern(1 0 1 0 1 0 1 0)) ///
    note("Robust standard errors clustered by districts and survey waves in parentheses.") ///
    se(2) b(3)
	
	
*------------------------------------------------------------
* Table A19: Conventional Wisdom – Economic Status
* Also export data for manuscript Figure 4
*------------------------------------------------------------
estimates clear

reghdfe econ_status displaced_pashtun nonpashtun_displaced nonpashtun_nondisplaced ///
    [pw=surv_weight], abs(PROVID m3) vce(cluster DISTID#m3)
estimates store m1

reghdfe econ_status displaced_pashtun nonpashtun_displaced nonpashtun_nondisplaced ///
    urban age yearsedu college unemployed elder headhousehold married intercommunal ///
    ln_insurgent_attacks_all [pw=surv_weight], abs(PROVID m3) vce(cluster DISTID#m3)
estimates store m2

* Export conventional wisdom results
esttab m1 m2, ///
    booktabs ///
    stats(provfe wavefe N, label("Province Fixed Effects?" "Survey Wave Fixed Effect?" "N")) ///
    varlabels(_cons Constant) ///
    starlevels(* 0.1 ** 0.05) ///
    mtitles($m1m2) ///
    nonumbers ///
    mgroups("Economic Status", pattern(1 0)) ///
    note("Robust standard errors clustered by districts and survey waves in parentheses.") ///
    se(2) b(3)

esttab m1 m2 using "replication/data/intermediate/FOGHORNresults_econstatus.csv", ///
    cells(b(fmt(3)) se(fmt(3))) ///
    label mlabels("econ1" "econ2") csv
	
	
*------------------------------------------------------------
* Section 3: District Level Analyses
*------------------------------------------------------------

*------------------------------------------------------------
* 1. Create district-level means of individual covariates
*------------------------------------------------------------
sort DISTID
by DISTID: egen mean_urban           = mean(urban)
by DISTID: egen mean_age             = mean(age)
by DISTID: egen mean_yearsedu        = mean(yearsedu)
by DISTID: egen mean_unemployed      = mean(unemployed)
by DISTID: egen mean_elder           = mean(elder)
by DISTID: egen mean_headhousehold   = mean(headhousehold)
by DISTID: egen mean_married         = mean(married)
by DISTID: egen mean_householdincome = mean(householdincome)
by DISTID: egen mean_econ_status     = mean(econ_status)
quietly by DISTID: gen dup_dist = cond(_N == 1, 0, _n)

*------------------------------------------------------------
* 2. Aggregate regressions (district means, no individual controls)
*------------------------------------------------------------
reghdfe intercommunal displaced_pashtun nonpashtun_displaced nonpashtun_nondisplaced ///
    if dup_dist == 1, abs(PROVID) vce(cluster PROVID)

*------------------------------------------------------------
* 3. Aggregate regressions (district means, with individual covariates)
*------------------------------------------------------------
reghdfe intercommunal displaced_pashtun nonpashtun_displaced nonpashtun_nondisplaced ///
    mean_urban mean_age mean_yearsedu mean_unemployed mean_elder mean_headhousehold ///
    mean_married mean_householdincome mean_econ_status if dup_dist == 1, ///
    abs(PROVID) vce(cluster PROVID)

*------------------------------------------------------------
* 4. Calculate residuals for key variables at individual level
*------------------------------------------------------------
reghdfe intercommunal urban age yearsedu college unemployed elder headhousehold married ///
    householdincome econ_status i.m24, abs(PROVID m3) vce(cluster DISTID#m3) resid
predict intercommunal_res, residuals

reghdfe ln_insurgent_attacks_all urban age yearsedu college unemployed elder headhousehold ///
    married householdincome econ_status i.m24, abs(PROVID m3) vce(cluster DISTID#m3) resid
predict insurgent_res, residuals

reghdfe soviet urban age yearsedu college unemployed elder headhousehold married ///
    householdincome econ_status i.m24, abs(PROVID m3) vce(cluster DISTID#m3) resid
predict soviet_res, residuals

reghdfe displaced_pashtun urban age yearsedu college unemployed elder headhousehold ///
    married householdincome econ_status i.m24, abs(PROVID m3) vce(cluster DISTID#m3) resid
predict displaced_pashtun_res, residuals

reghdfe nonpashtun_displaced urban age yearsedu college unemployed elder headhousehold ///
    married householdincome econ_status i.m24, abs(PROVID m3) vce(cluster DISTID#m3) resid
predict nonpashtun_displaced_res, residuals

reghdfe nonpashtun_nondisplaced urban age yearsedu college unemployed elder headhousehold ///
    married householdincome econ_status i.m24, abs(PROVID m3) vce(cluster DISTID#m3) resid
predict nonpashtun_nondisplaced_res, residuals

*------------------------------------------------------------
* 5. District-level means of residuals
*------------------------------------------------------------
sort DISTID
by DISTID: egen mean_intercommunal_res           = mean(intercommunal_res)
by DISTID: egen mean_insurgent_res               = mean(insurgent_res)
by DISTID: egen mean_soviet_res                  = mean(soviet_res)
by DISTID: egen mean_displaced_pashtun_res       = mean(displaced_pashtun_res)
by DISTID: egen mean_nonpashtun_displaced_res    = mean(nonpashtun_displaced_res)
by DISTID: egen mean_nonpashtun_nondisplaced_res = mean(nonpashtun_nondisplaced_res)

*------------------------------------------------------------
* 6. Panel setup and district-level regressions with bootstrapped SEs
*------------------------------------------------------------
xtset PROVID
set seed 123

xtreg mean_intercommunal_res c.mean_displaced_pashtun_res /// 
      c.mean_nonpashtun_displaced_res c.mean_nonpashtun_nondisplaced_res /// 
      if dup_dist == 1, fe vce(boot, reps(1000))
estimates store mA_1

xtreg mean_insurgent_res c.mean_displaced_pashtun_res /// 
      c.mean_nonpashtun_displaced_res c.mean_nonpashtun_nondisplaced_res /// 
      if dup_dist == 1, fe vce(boot, reps(1000))
estimates store mB_1

xtreg mean_soviet_res c.mean_displaced_pashtun_res /// 
      c.mean_nonpashtun_displaced_res c.mean_nonpashtun_nondisplaced_res /// 
      if dup_dist == 1, fe vce(boot, reps(1000))
estimates store mC_1

*------------------------------------------------------------
* Table A14: District-Level Analyses on Soviet and US Influence
*------------------------------------------------------------
esttab mC_1 mB_1 mA_1, ///
    booktabs ///
    stats(provfe N, label("Province Fixed Effects?" "N")) ///
    varlabels(_cons Constant) ///
    starlevels(* 0.1 ** 0.05) ///
    mtitles() ///
    nonumbers ///
    se(2) b(3) ///
    note("Bootstrap standard errors, with 1000 replications, in parentheses.")

* Export results for plotting Figure A21 in R
esttab mA_1 mB_1 mC_1 using "replication/data/intermediate/FOGHORNresults_violence.csv", ///
    cells(b(fmt(3)) se(fmt(3))) ///
    label mlabels("intercommunal" "sigacts" "soviet") csv

*------------------------------------------------------------
* Table A13: Pearson Correlation Coefficient with P‐values
*------------------------------------------------------------
sort PROVID DISTID
by PROVID DISTID: keep if _n == 1

pwcorr soviet intercommunal, sig
pwcorr ln_insurgent_attacks_all intercommunal, sig


